package com.gly091020.config;

import com.gly091020.ChineseChest;
import com.gly091020.ChineseChestFun;
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.clothconfig2.api.ConfigBuilder;
import me.shedaniel.clothconfig2.api.ConfigCategory;
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
import me.shedaniel.clothconfig2.impl.builders.DropdownMenuBuilder;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

import java.util.Optional;

public class ChineseChestModMenuIntegration implements ModMenuApi {
    @Override
    public ConfigScreenFactory<?> getModConfigScreenFactory() {
        return parent -> {
            ConfigBuilder builder = ConfigBuilder.create()
                    .setParentScreen(parent)
                    .setTitle(Text.translatable("title.chinese_chest.config"))
                    .setDefaultBackgroundTexture(new Identifier(ChineseChest.ModID,  ChineseChestFun.isGLY() ? "textures/gui/gly091020.png" : "textures/gui/chest.png"));
            ConfigCategory general = builder.getOrCreateCategory(Text.translatable("title.englishfight.config"));
            ConfigEntryBuilder entryBuilder = builder.entryBuilder();
            general.addEntry(entryBuilder.startBooleanToggle(
                    Text.translatable("config.chinese_chest.is_enable"),
                    ChineseChest.config.isEnable).setDefaultValue(true)
                    .setSaveConsumer(aBoolean -> ChineseChest.config.isEnable = aBoolean)
                    .build()
            );
            if(ChineseChestFun.isGLY()){
                general.addEntry(entryBuilder.startBooleanToggle(
                                Text.translatable("config.chinese_chest.gly_mode"),
                                ChineseChest.config.glyMode).setDefaultValue(false)
                        .setSaveConsumer(aBoolean -> ChineseChest.config.glyMode = aBoolean)
                        .build()
                );
            }
            general.addEntry(entryBuilder.startFloatField(
                    Text.translatable("config.chinese_chest.multiple"),
                    ChineseChest.config.baseMultiple
                    ).setMin(0).setDefaultValue(2)
                    .setTooltip(Text.translatable("config.chinese_chest.multiple.tip"))
                    .setSaveConsumer(aFloat -> ChineseChest.config.baseMultiple = aFloat).build()
            );
            general.addEntry(entryBuilder.startDropdownMenu(
                    Text.translatable("config.chinese_chest.poem_file"),
                    DropdownMenuBuilder.TopCellElementBuilder.of(ChineseChest.config.poemFile, x->x),
                    DropdownMenuBuilder.CellCreatorBuilder.of(Text::of)
            ).setSaveConsumer(x->ChineseChest.config.poemFile = x)
                    .setSelections(ChineseChestFun.getChineseFiles())
                    .setDefaultValue("default.json")
                    .setErrorSupplier(s -> {
                        if (!FabricLoader.getInstance().getConfigDir().resolve("ChineseChestFiles").resolve(s).toFile().isFile()) {
                            return Optional.of(Text.translatable("config.chinese_chest.poem_file.no_file"));
                        }
                        return Optional.empty();
                    })
                    .build());
            builder.setSavingRunnable(() -> {
                AutoConfig.getConfigHolder(ChineseChestConfig.class).setConfig(ChineseChest.config);
                AutoConfig.getConfigHolder(ChineseChestConfig.class).save();
                ChineseChestFun.initPoems(FabricLoader.getInstance().getConfigDir().resolve("ChineseChestFiles")
                        .resolve(ChineseChest.config.poemFile));
            });
            return builder.build();
        };
    }
}
